package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.LineaProduccion;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOLineaProduccion;

public class MySQLDAOLineaProduccion implements DAOLineaProduccion {
	
	private LineaProduccion lineaproduccion = null;
	
	
	public MySQLDAOLineaProduccion() {
	}

	public int insert(LineaProduccion lineaproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into LineaProduccion ("
					+ "Id_Linea, Nombre "
					+ ") " + "values (?, ?) ");

			stmt.setLong(1, lineaproduccion.getNroLinea());
			stmt.setString(2, lineaproduccion.getNombre());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(LineaProduccion lineaproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update LineaProduccion " 
							+ "set Nombre = ?"
							+ " where id_LineaProduccion = ? ");

			stmt.setString(1, lineaproduccion.getNombre());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(LineaProduccion lineaproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from LineaProduccion where id_LineaProduccion = ? ");
			stmt.setLong(1, lineaproduccion.getNroLinea());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<LineaProduccion> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<LineaProduccion> lineaproduccions = new ArrayList<LineaProduccion>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from LineaProduccion");
			rs = stmt.executeQuery();

			while (rs.next()) {
				LineaProduccion lineaproduccion = new LineaProduccion();
				lineaproduccion.setNroLinea(rs.getInt("Id_Linea"));
				lineaproduccion.setNombre(rs.getString("Nombre"));
				lineaproduccions.add(lineaproduccion);
			}

			return lineaproduccions;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public LineaProduccion findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		lineaproduccion = new LineaProduccion();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from LineaProduccion where Id_Linea = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			lineaproduccion.setNombre(rs.getString("Nombre"));
   		lineaproduccion.setNroLinea(rs.getInt("Id_Linea"));

			}

			return lineaproduccion;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(LineaProduccion lineaproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from LineaProduccion where UCASE(Nombre)");

			stmt.setString(1, lineaproduccion.getNombre().toUpperCase());
//			stmt.setString(2, lineaproduccion.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
